package Pre.Work;

import java.util.*;

public class Exam01 implements Comparable {
    //创建Student类，属性包括id[1-40],scroe[60-100], id属性⾃增，成绩属性随机⽣成。
    // 创建集合，保存20个对象，找到分数最⾼与最低的学⽣，使⽤List集合实现。
    private int id;
    private int score;

//    public Exam01(int id, int score) {
//        this.id = id;
//        this.score = score;
//    }
    private static int i = 1;

    public Exam01() {
        this.id = i++;
        this.score= new Random().nextInt(60, 100);
    }


    public int getId() {
        return id;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Exam01{" +
                "学号：" + id +
                ", 成绩：" + score +
                '}';
    }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Exam01 exam01 = (Exam01) o;
        return id == exam01.id && score == exam01.score;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, score);
    }

    @Override
    public int compareTo(Object o) { // 重写compareTo方法
        if (o == null ) {
            throw new NullPointerException();
        }
        if (this.equals(o)) {
            return 0;
        }
        if (o instanceof Exam01 exam01) {
            return score - exam01.getScore() > 0 ? 1 : -1;
        }
        throw new ClassCastException();
    }
}
class Student{
    TreeSet list = new TreeSet();

    TreeSet<Exam01> treeSet = new TreeSet<>(new Comparator<Exam01>() {
        @Override
        public int compare(Exam01 o1, Exam01 o2) {
            return o1.getScore() - o2.getScore();
        }
    });

    public void addList(int num){
//        for (int i = 0; i < num ; i++) {
//            Random random = new Random();
//            int score = random.nextInt(41) + 60 ;
//            list.add(new Exam01(i,score));
//        }
        for (int i = 0; i < num; i++) {
            treeSet.add(new Exam01());
        }
    }


    public void showAll(){
        list.forEach(obj -> System.out.println(obj));
    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.addList(20);
        stu.showAll();
//        System.out.println("最高分：" + stu.list.first());
//        System.out.println("最低分：" + stu.list.last());
        System.out.println(stu.treeSet);
    }
}
